﻿using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Math.Geometry;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using JilyHelper;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace GameControler
{
    public static class HandleImageHelper
    {
        public static bool JudgeView(this IntPtr handle)
        {
            var rect = new GameControler.NativeHelper.RECT();
            var result = NativeHelper.GetWindowRect(handle, ref rect);
            if (result != IntPtr.Zero)
            {
                Rectangle r = new Rectangle(new System.Drawing.Point(rect.Left + 662, rect.Top + 373), new Size(134, 134));
                Rectangle rnull = new Rectangle(new System.Drawing.Point(rect.Left + 1133, rect.Top + 42), new Size(282, 801));

                using (var img = new Bitmap(rnull.Width, rnull.Height))
                {
                    using (var g = Graphics.FromImage(img))
                    {
                        g.CopyFromScreen(rnull.Location, System.Drawing.Point.Empty, rnull.Size, CopyPixelOperation.SourceCopy);
                        g.DrawImage(img, 0, 0, img.Width, img.Height);

                        var isnull = JudgeNull(img);
                        if (!isnull)
                        {
                            return false;
                        }
                    }
                }

                using (var img = new Bitmap(r.Width, r.Height))
                {
                    using (var g = Graphics.FromImage(img))
                    {
                        g.CopyFromScreen(r.Location, System.Drawing.Point.Empty, r.Size, CopyPixelOperation.SourceCopy);
                        g.DrawImage(img, 0, 0, img.Width, img.Height);
                        return ProcessImage(img);
                    }
                }
            }

            return false;
        }

        private static bool JudgeNull(Bitmap bitmap)
        {
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            ColorFiltering colorFilter = new ColorFiltering();
            colorFilter.Red = new IntRange(0, 64);
            colorFilter.Green = new IntRange(0, 64);
            colorFilter.Blue = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;
            colorFilter.ApplyInPlace(bitmapData);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = 5;
            blobCounter.MinWidth = 5;
            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            return blobs.Length == 0;
        }

        private static bool ProcessImage(Bitmap bitmap)
        {
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            ColorFiltering colorFilter = new ColorFiltering();
            colorFilter.Red = new IntRange(0, 64);
            colorFilter.Green = new IntRange(0, 64);
            colorFilter.Blue = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;
            colorFilter.ApplyInPlace(bitmapData);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = 5;
            blobCounter.MinWidth = 5;
            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            //Graphics g = Graphics.FromImage(bitmap);
            //Pen redPen = new Pen(Color.Red, 2);

            int matchcount = 0;
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                var bolb = blobs[i];
                if (bolb.Area > 8000 && bolb.Area < 22500)
                {
                    List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                    List<IntPoint> corners;
                    if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                    {
                        if (corners.Count == 4)
                        {
                            PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
                            if (subType == PolygonSubType.Square || subType == PolygonSubType.Rectangle)
                            {
                                matchcount++;
                                //Pen pen = redPen;
                                //g.DrawPolygon(pen, ToPointsArray(corners));

                                //bitmap.Save(@"C://1.png");

                            }
                        }
                    }
                }
            }

            //redPen.Dispose();
            //g.Dispose();

            return matchcount == 1;
        }

        private static System.Drawing.Point[] ToPointsArray(List<IntPoint> points)
        {
            System.Drawing.Point[] array = new System.Drawing.Point[points.Count];
            for (int i = 0, n = points.Count; i < n; i++)
            {
                array[i] = new System.Drawing.Point(points[i].X, points[i].Y);
            }
            return array;
        }

    }
}
